package com.tanhua.server.controller;

import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

// 圈子中评论的功能
@RestController
@RequestMapping("comments")
public class QuanZiCommentController{
    @Autowired
    private QuanZiService quanZiService;

    // 查询评论列表
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId")String publishId,
                                                        @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult = quanZiService.queryCommentList(publishId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 保存评论
    public ResponseEntity<Void> saveComment(@RequestBody Map<String,String> param){
        try {
            String publishId = param.get("movementId");
            String content = param.get("comment");
            Boolean result = quanZiService.saveComments(publishId,content);
            if (result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 评论点赞
    @GetMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id")String publishId){
        try {
            Long likecount = quanZiService.likeComment(publishId);
            if (likecount != null){
                return ResponseEntity.ok(likecount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 取消评论点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount = quanZiService.dislikeComment(publishId);
            if (likeCount != null){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
